package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exampleyyy.demoyyy.JieKouService.ShoppingCartServiceJieKou;
import com.exampleyyy.demoyyy.SHITIYYY.ShoppingCart;
import com.exampleyyy.demoyyy.common.BaseContext;
import com.exampleyyy.demoyyy.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartServiceJieKou shoppingcartservicejiekou;

    @PostMapping("/add")
    public R<Object> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是水果
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前水果或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingcartservicejiekou.getOne(queryWrapper);
        System.out.println("购物车数量是");
        System.out.println(cartServiceOne);
        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingcartservicejiekou.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingcartservicejiekou.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(1,"添加购物车成功",cartServiceOne);
    }


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<Object> list(){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        System.out.println("-----------BaseContext.getCurrentId()---------------");
        System.out.println(BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingcartservicejiekou.list(queryWrapper);
        System.out.println("-------------------------");
        System.out.println(list);
        return R.success(1,"购物车查询成功",list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<Object> clean(){
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingcartservicejiekou.remove(queryWrapper);
        return R.success(1,"清空购物车成功",null);
    }



    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        System.out.println("-------当前currentId是------------:");
        System.out.println(currentId);

        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId != null){
            //添加到购物车的是水果
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne1 = shoppingcartservicejiekou.getOne(queryWrapper);


        //如果已经存在，就在原来数量基础上加一
        //先获取number的数据
        Integer number = cartServiceOne1.getNumber();
        if (number!=1){//如果不等于1 比如等于2345 就正常减1
            cartServiceOne1.setNumber(number - 1);
            shoppingcartservicejiekou.updateById(cartServiceOne1);
        }else {//如果等于1 点击了减号先设为0在删除 因为前端代码是根据number的值显示“加减”还是“选择规格“ 又兴趣的可以看前端代码174行那几行
            Long id = cartServiceOne1.getId();
            cartServiceOne1.setNumber(0);//这句很关键 删除前先把number设为0 前端根据这个判断显示
            shoppingcartservicejiekou.removeById(id);
        }
        return R.success(1,"减少成功",cartServiceOne1);
    }


}
